using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Group : MonoBehaviour
{
    float lastFall = 0, speedTime = 0.5f, autoDownTime = 1;
    // Start is called before the first frame update
    void Start()
    {
        if (!isValidGridPos())
        {
            Debug.Log("Game Over!");
            Destroy(gameObject);
        }
        lastFall = Time.time;
        
    }

    // Update is called once per frame
    void Update()
    {
        //move left
        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            transform.position += new Vector3(-1, 0, 0);
            if (isValidGridPos())
            {
                updateGrid();
            }
            else
            {
                transform.position += new Vector3(1, 0, 0);
            }
        }
        else if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            transform.position += new Vector3(1, 0, 0);
            if (isValidGridPos())
                updateGrid();
            else
                transform.position += new Vector3(-1, 0, 0);
        }
        else if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            transform.Rotate(0, 0, -90);

            if (isValidGridPos())
            {
                updateGrid();
            }
            else
            {
                transform.Rotate(0, 0, 90);
            }
        }else if (Input.GetKeyDown(KeyCode.DownArrow) || Time.time - lastFall >= autoDownTime)
        {
            downCube();
            lastFall = Time.time;
        }else if(Input.GetKey("down"))
        {
            
            if (Time.time - lastFall > speedTime)
            {
                speedTime -= 0.01 > 0 ? speedTime : 0;
                downCube();
            }
            
        }else
        {
            speedTime = 0.5f;
        }

        

        
    }

    bool isValidGridPos()
    {
        foreach(Transform child in transform)
        {
            Vector2 v = Playfield.roundVec2(child.position);

            //not inside Border?
            if (!Playfield.inisideBorder(v))
            {
                return false;
            }

            //Block in grid cell (and not part of same group)?
            if(Playfield.grid[(int)v.x,(int)v.y] != null && Playfield.grid[(int)v.x,(int)v.y].parent != transform)
            {
                return false;
            }
        }
        return true;
    }

    void updateGrid()
    {
        // Remove old children from grid
        for(int y = 0; y < Playfield.h; ++y)
        {
            for(int x = 0; x < Playfield.w; ++x)
            {
                if(Playfield.grid[x,y] != null)
                {
                    if(Playfield.grid[x,y].parent == transform)
                    {
                        Playfield.grid[x, y] = null;
                    }
                }
            }
        }

        // Add new children to grid
        foreach(Transform child in transform)
        {
            Vector2 v = Playfield.roundVec2(child.position);
            Playfield.grid[(int)v.x, (int)v.y] = child;
        }
    }

    void downCube()
    {
        transform.position += new Vector3(0, -1, 0);

        if (isValidGridPos())
        {
            updateGrid();

        }
        else
        {
            transform.position += new Vector3(0, 1, 0);
            Playfield.deleteFullRows();
            FindObjectOfType<Spawner>().spawnNext();
            enabled = false;
            
        }
    }
}
